package com.example.demo.service.impl;

import com.example.demo.service.BloomFilterService;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Code Farmer
 * @date 2020/4/23 19:57
 */
@Service
public class BloomFilterServiceImpl implements BloomFilterService {

    //size 预计要插入多少数据
    //fpp 容错率
    private static int size = 100000;

    private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), size, 0.2);

    public void bloomFilter() {
        for (int i = 0; i < size; i++) {
            bloomFilter.put(i);
        }
        List<Integer> list = new ArrayList<>(10000);
        for (int i = size + 10000; i < size + 20000; i++) {
            if (bloomFilter.mightContain(i)) { // 误判
                list.add(i);
            }
        }

        System.out.println("误判的数量：" + list.size());

    }

    @Override
    public void test() {

    }

    public static void main(String[] args) {
        new BloomFilterServiceImpl().bloomFilter();
    }

}
